package ca.seia.comp6521.a2.itemsetalgorithms;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;

import ca.seia.comp6521.a2.hashstorage.HashBitVector;
import ca.seia.comp6521.a2.hashstorage.HashBucket;
import ca.seia.comp6521.a2.hashstorage.PairCountHashStrategy;
import ca.seia.comp6521.a2.hashstorage.PairMultiHashStrategy;
import ca.seia.comp6521.a2.hashstorage.PairPrimeHashStrategy;
import ca.seia.comp6521.a2.io.DataFileReader;
import ca.seia.comp6521.a2.io.DataFileWriter;
import ca.seia.comp6521.a2.io.FileReadResult;
import ca.seia.comp6521.a2.io.ObjectSerializer;
import ca.seia.comp6521.a2.itemsetstorage.CharTriplet;
import ca.seia.comp6521.a2.itemsetstorage.DynamicTriangularMatrix;
import ca.seia.comp6521.a2.itemsetstorage.IntItemCountStorage;
import ca.seia.comp6521.a2.itemsetstorage.IntItems;
import ca.seia.comp6521.a2.itemsetstorage.IntPair;
import ca.seia.comp6521.a2.itemsetstorage.IntTriplet;

public class Apriori {
	
	public static final String RESULTS_FILE_PATH = "c:\\Temp\\results.txt";
	public File transactionFile;
	private DataFileWriter fileWriter;
	public int supportPercentage;
	public int support;
	
	public Apriori(File transactionFile,DataFileWriter fileWriter, int supportPercentage) throws IOException {
		this.transactionFile = transactionFile;
		this.supportPercentage = supportPercentage;
		this.fileWriter = fileWriter;
	}
	
	public Apriori(File transactionFile, int supportPercentage) throws IOException {
		this.transactionFile = transactionFile;
		this.supportPercentage = supportPercentage;
		this.fileWriter = new DataFileWriter(RESULTS_FILE_PATH);
	}
	
	public void getFrequentItemSets() throws IOException {
		PassIResult passIResults = executePassI();
		
		if (passIResults.getBitVector().getNbrFrequentItems() < 2) {
			return;
		}
		
		PassIIResult passIIResult = executePassII(passIResults);
		if (passIIResult.getCandidateTriplets().isEmpty()) {
			return;
		} 
		
//		if (passIIResult.getUniqueCandidateTriplets().isEmpty()) {
//			return;
//		} 
		
		PassNResult passIIIResult = executePassIII(passIIResult);
		if (passIIIResult.getCandidateFrequentItems().isEmpty()) {
			return;
		}
//		if (passIIIResult.getUniqueCandidateFrequentItems().isEmpty()) {
//			return;
//		}
		PassNResult previousPassResult = passIIIResult;
		
		do {
			previousPassResult = executePassN(previousPassResult);
		} while (!previousPassResult.getCandidateFrequentItems().isEmpty());
//		} while (!previousPassResult.getUniqueCandidateFrequentItems().isEmpty());
	}
	
	public void getFrequentItemSetsWithIntCompression() throws IOException {
		PassIResult passIResults = executePassI();
		
		if (passIResults.getBitVector().getNbrFrequentItems() < 2) {
			return;
		}
		
		PassIIResult passIIResult = executePassIICompressed(passIResults);
		if (passIIResult.getCandidateIntTriplets().isEmpty()) {
			return;
		} 
		
		PassNResult passIIIResult = executePassIIICompressed(passIIResult);
		if (passIIIResult.getCandidateFrequentItems().isEmpty()) {
			return;
		}
		PassNResult previousPassResult = passIIIResult;
		
		do {
			previousPassResult = executePassN(previousPassResult);
		} while (!previousPassResult.getCandidateFrequentItems().isEmpty());
	}
	
	public void getFrequentItemSetsWithCharCompression() throws IOException {
		PassIResult passIResults = executePassI();
		
		if (passIResults.getBitVector().getNbrFrequentItems() < 2) {
			return;
		}
		
		PassIIResult passIIResult = executePassIICharCompressed(passIResults);
		if (passIIResult.getCandOptiCharTriplets().isEmpty()) {
			return;
		} 
		
		PassNResult passIIIResult = executePassIIICharCompressed(passIIResult);
		if (passIIIResult.getCandidateFrequentItems().isEmpty()) {
			return;
		}
		PassNResult previousPassResult = passIIIResult;
		
		do {
			previousPassResult = executePassN(previousPassResult);
		} while (!previousPassResult.getCandidateFrequentItems().isEmpty());
	}
	
	public void getFrequentItemSetsWithCharFileStorageCompression() throws Exception {
		PassIResult passIResults = executePassI();
		
		if (passIResults.getBitVector().getNbrFrequentItems() < 2 || this.support == 0) {
			return;
		}
		
		PassIIResult passIIResult = executePassIICharCompressedFileStorage((passIResults));
		if (passIIResult.getCandOptiCharTriplets().isEmpty()) {
			return;
		} 
		
		PassNResult passIIIResult = executePassIIICharFileStorageCompressed(passIIResult);
		if (passIIIResult.getCandidateFrequentItems().isEmpty()) {
			return;
		}
		PassNResult previousPassResult = passIIIResult;
		
		do {
			previousPassResult = executePassN(previousPassResult);
		} while (!previousPassResult.getCandidateFrequentItems().isEmpty());
	}
	
	public void getFrequentItemSetsPCY() throws Exception {
		PassIResult passIResults = executePassIPCY();
		
		if (passIResults.getBitVector().getNbrFrequentItems() < 2) {
			return;
		}
		
		PassIIResult passIIResult = executePassIIPCYCharCompressedFileStorage((passIResults));
		if (passIIResult.getCandOptiCharTriplets().isEmpty()) {
			return;
		} 
		PassNResult passIIIResult = executePassIIICharFileStorageCompressed(passIIResult);
		if (passIIIResult.getCandidateFrequentItems().isEmpty()) {
			return;
		}
		PassNResult previousPassResult = passIIIResult;
		
		do {
			previousPassResult = executePassN(previousPassResult);
		} while (!previousPassResult.getCandidateFrequentItems().isEmpty());
	}
	

	public PassIResult executePassI() throws FileNotFoundException {
		DataFileReader passReader = new DataFileReader(transactionFile.getAbsolutePath());
		long startTime = System.currentTimeMillis();
//		System.out.println("Doing pass I");
		FileReadResult readResults = passReader.readTransactions();
//		System.out.println(readResults); //TODO comment 
		int support = calculateSupport(supportPercentage, readResults.getTransactionCount());
		this.support = support;
		BitVector bitVector = generateBitVector(readResults.getItemCount(), support);
//		System.out.println("support " + support); //TODO comment
//		System.out.println(bitVector); //TODO comment 
		long executionTime = System.currentTimeMillis() - startTime;
		PassIResult passIResult = new PassIResult(support, readResults.getItemCount(),executionTime,bitVector);
		passIResult.setTotalTransactionLines(readResults.getTransactionCount());
//		System.out.println("number of transactions in file " + readResults.getTransactionCount());
//		System.out.println("ExecutionTime for passI = " + executionTime + " ms");
		return passIResult;
	}
	
	
	public PassIResult executePassIPCY() throws FileNotFoundException {
		DataFileReader passReader = new DataFileReader(transactionFile.getAbsolutePath());
		long startTime = System.currentTimeMillis();
		HashBucket hashbucketOne = new HashBucket(new PairCountHashStrategy(), 90000);
		HashBucket hashbucketTwo = new HashBucket(new PairPrimeHashStrategy(), 80000);
		HashBucket hashbucketThree = new HashBucket(new PairMultiHashStrategy(), 80000);
		
		HashBucket[] hashBuckets = new HashBucket[]{hashbucketOne,hashbucketTwo,hashbucketThree};
		
//		FileReadResult readResults = passReader.readTransactions();
		FileReadResult readResults = passReader.readTransactionsPCY(hashBuckets);
//		System.out.println(readResults); //TODO comment 
		int support = calculateSupport(supportPercentage, readResults.getTransactionCount());
		this.support = support;
		BitVector bitVector = generateBitVector(readResults.getItemCount(), support);
		System.out.println("support " + support); //TODO comment
//		System.out.println(bitVector); //TODO comment 
		long executionTime = System.currentTimeMillis() - startTime;
		PassIResult passIResult = new PassIResult(support, readResults.getItemCount(),executionTime,bitVector);
		passIResult.setTotalTransactionLines(readResults.getTransactionCount());
		passIResult.setHashBitVectors(generateHashBitVectors(hashBuckets, support));
		//passIResult.setHashBuckets(hashBuckets);
		System.out.println("number of transactions in file " + readResults.getTransactionCount());
		System.out.println("ExecutionTime for passI = " + executionTime + " ms");
//		System.out.println(hashbucketOne);
//		System.out.println(hashbucketTwo);
//		System.out.println(hashbucketThree);
		hashBuckets = null;
		hashbucketOne = null;
		hashbucketTwo = null;
		return passIResult;
	}
	
	public PassIIResult executePassIIPCY(PassIResult passIResults) throws IOException {
		DataFileReader passReader = new DataFileReader(transactionFile.getAbsolutePath());
		BitVector bitVector = passIResults.getBitVector();
		IdTranslationResult idTranslationResult = bitVector.generateRenamedFrequentIdMaps();
		DynamicTriangularMatrix storage = new DynamicTriangularMatrix(idTranslationResult.getIdMap(),
																	  idTranslationResult.getReversedIdMap(),
																	  bitVector.getNbrFrequentItems());
		
		long startTime = System.currentTimeMillis();
		System.out.println("Doing pass II");
		passReader.readTransactionsPassIIPCY(passIResults, storage);
		
		List<IntPair>  frequentPairs = storage.generateFrequentPairs(this.support);
		storage.writeToDisk(frequentPairs, fileWriter);
		List<IntItems> candidateTriplets = storage.generateCandidateTriplets(frequentPairs);

		long executionTime = System.currentTimeMillis() - startTime;
		System.out.println("ExecutionTime for passII = " + executionTime + " ms");
		//System.out.println("Real Frequent pairs " + storage );
//		System.out.println("candidateTriplets are " + candidateTriplets);
		PassIIResult passIIResult = new PassIIResult(candidateTriplets, executionTime);
		passIIResult.setTotalTransactionLines(passIResults.getTotalTransactionLines());
		passIIResult.setSupport(this.support);
		storage = null;
		return passIIResult;
	}
	
	
	public PassIIResult executePassIIPCYCharCompressedFileStorage(PassIResult passIResults) throws IOException {
		DataFileReader passReader = new DataFileReader(transactionFile.getAbsolutePath());
		BitVector bitVector = passIResults.getBitVector();
		IdTranslationResult idTranslationResult = bitVector.generateRenamedFrequentIdMaps();
		DynamicTriangularMatrix storage = new DynamicTriangularMatrix(idTranslationResult.getIdMap(),
																	  idTranslationResult.getReversedIdMap(),
																	  bitVector.getNbrFrequentItems());
		
		long startTime = System.currentTimeMillis();
		System.out.println("Doing pass II");
		passReader.readTransactionsPassIIPCY(passIResults, storage);
		
		List<IntPair>  frequentPairs = storage.generateFrequentPairs(this.support);
		System.out.println("size of frequent pairs is " + frequentPairs.size());
		storage.writeToDisk(frequentPairs, fileWriter);
		storage = null;
		ObjectSerializer objectSerializer = new ObjectSerializer();
		CandidateGenerator.objectSerializer = objectSerializer;
		List<CharTriplet> candidateTriplets = CandidateGenerator.generateCandidateCharTripletsStoreToDisk(frequentPairs);
		long executionTime = System.currentTimeMillis() - startTime;
		System.out.println("ExecutionTime for passII = " + executionTime + " ms");
//		System.out.println("Real Frequent pairs " + storage );
//		PassIIResult passIIResult = new PassIIResult(candidateTriplets, executionTime);
		PassIIResult passIIResult = new PassIIResult();
		passIIResult.setCandOptiCharTriplets(candidateTriplets);
		passIIResult.setTotalTransactionLines(passIResults.getTotalTransactionLines());
		passIIResult.setSupport(this.support);
		return passIIResult;
	}
	
	public PassIIResult executePassII(PassIResult passIResults) throws IOException {
		DataFileReader passReader = new DataFileReader(transactionFile.getAbsolutePath());
//		Triples storage = new Triples();
//		TriangularMatrix mat = new TriangularMatrix(1966);
		BitVector bitVector = passIResults.getBitVector();
		IdTranslationResult idTranslationResult = bitVector.generateRenamedFrequentIdMaps();
		DynamicTriangularMatrix storage = new DynamicTriangularMatrix(idTranslationResult.getIdMap(),
																	  idTranslationResult.getReversedIdMap(),
																	  bitVector.getNbrFrequentItems());
		
		long startTime = System.currentTimeMillis();
		System.out.println("Doing pass II");
		passReader.readTransactions(passIResults, storage);
//		System.out.println("Candidates pairs " + storage);
//		System.out.println("candidateTriplets are " + candidateTriplets);
		List<IntPair>  frequentPairs = storage.generateFrequentPairs(this.support);
		storage.writeToDisk(frequentPairs, fileWriter);
		List<IntItems> candidateTriplets = storage.generateCandidateTriplets(frequentPairs);
//		Set<IntItems> candidateTriplets = storage.generateCandidateTripletsWithNoDuplicates(frequentPairs);
//		List<IntTriplet> candidateTriplets = storage.generateCandidateIntTriplets(frequentPairs);
		storage = null;
		long executionTime = System.currentTimeMillis() - startTime;
		System.out.println("ExecutionTime for passII = " + executionTime + " ms");
//		System.out.println("Real Frequent pairs " + storage );
		PassIIResult passIIResult = new PassIIResult(candidateTriplets, executionTime);
		passIIResult.setTotalTransactionLines(passIResults.getTotalTransactionLines());
		passIIResult.setSupport(this.support);
		return passIIResult;
	}
	
	public PassIIResult executePassIICompressed(PassIResult passIResults) throws IOException {
		DataFileReader passReader = new DataFileReader(transactionFile.getAbsolutePath());
		BitVector bitVector = passIResults.getBitVector();
		IdTranslationResult idTranslationResult = bitVector.generateRenamedFrequentIdMaps();
		DynamicTriangularMatrix storage = new DynamicTriangularMatrix(idTranslationResult.getIdMap(),
																	  idTranslationResult.getReversedIdMap(),
																	  bitVector.getNbrFrequentItems());
		
		long startTime = System.currentTimeMillis();
		System.out.println("Doing pass II");
		passReader.readTransactions(passIResults, storage);
//		System.out.println("Candidates pairs " + storage);
		List<IntPair>  frequentPairs = storage.generateFrequentPairs(this.support);
		System.out.println("size of frequent pairs is " + frequentPairs.size());
		storage.writeToDisk(frequentPairs, fileWriter);
		storage = null;
		List<IntTriplet> candidateTriplets = CandidateGenerator.generateCandidateIntTriplets(frequentPairs);
		long executionTime = System.currentTimeMillis() - startTime;
		System.out.println("ExecutionTime for passII = " + executionTime + " ms");
//		System.out.println("Real Frequent pairs " + storage );
		PassIIResult passIIResult = new PassIIResult();
		passIIResult.setCandidateIntTriplets(candidateTriplets);
		passIIResult.setTotalTransactionLines(passIResults.getTotalTransactionLines());
		passIIResult.setSupport(this.support);
		return passIIResult;
	}
	
	public PassIIResult executePassIICharCompressed(PassIResult passIResults) throws IOException {
		DataFileReader passReader = new DataFileReader(transactionFile.getAbsolutePath());
		BitVector bitVector = passIResults.getBitVector();
		IdTranslationResult idTranslationResult = bitVector.generateRenamedFrequentIdMaps();
		DynamicTriangularMatrix storage = new DynamicTriangularMatrix(idTranslationResult.getIdMap(),
																	  idTranslationResult.getReversedIdMap(),
																	  bitVector.getNbrFrequentItems());
		
		long startTime = System.currentTimeMillis();
//		System.out.println("Doing pass II");
		passReader.readTransactions(passIResults, storage);
//		System.out.println("Candidates pairs " + storage);
		List<IntPair>  frequentPairs = storage.generateFrequentPairs(this.support);
		System.out.println("size of frequent pairs is " + frequentPairs.size());
		storage.writeToDisk(frequentPairs, fileWriter);
		storage = null;
		List<CharTriplet> candidateTriplets = CandidateGenerator.generateCandidateCharTriplets(frequentPairs);
		long executionTime = System.currentTimeMillis() - startTime;
//		System.out.println("ExecutionTime for passII = " + executionTime + " ms");
//		System.out.println("Real Frequent pairs " + storage );
//		PassIIResult passIIResult = new PassIIResult(candidateTriplets, executionTime);
		PassIIResult passIIResult = new PassIIResult();
		passIIResult.setCandOptiCharTriplets(candidateTriplets);
		passIIResult.setTotalTransactionLines(passIResults.getTotalTransactionLines());
		passIIResult.setSupport(this.support);
		return passIIResult;
	}
	
	public PassIIResult executePassIICharCompressedFileStorage(PassIResult passIResults) throws IOException {
		DataFileReader passReader = new DataFileReader(transactionFile.getAbsolutePath());
		BitVector bitVector = passIResults.getBitVector();
		IdTranslationResult idTranslationResult = bitVector.generateRenamedFrequentIdMaps();
		DynamicTriangularMatrix storage = new DynamicTriangularMatrix(idTranslationResult.getIdMap(),
																	  idTranslationResult.getReversedIdMap(),
																	  bitVector.getNbrFrequentItems());
		
//		long startTime = System.currentTimeMillis();
//		System.out.println("Doing pass II");
		passReader.readTransactions(passIResults, storage);
//		System.out.println("Candidates pairs " + storage);
//		List<IntPair>  frequentPairs = storage.generateFrequentPairs(this.support);
		List<IntPair>  frequentPairs = storage.generateFrequentPairsAndWriteToDisk(this.support,fileWriter);
//		System.out.println("size of frequent pairs is " + frequentPairs.size());
//		storage.writeToDisk(frequentPairs, fileWriter);
		storage = null;
		ObjectSerializer objectSerializer = new ObjectSerializer();
		CandidateGenerator.objectSerializer = objectSerializer;
		List<CharTriplet> candidateTriplets = CandidateGenerator.generateCandidateCharTripletsStoreToDisk(frequentPairs);
//		long executionTime = System.currentTimeMillis() - startTime;
//		System.out.println("ExecutionTime for passII = " + executionTime + " ms");
		PassIIResult passIIResult = new PassIIResult();
		passIIResult.setCandOptiCharTriplets(candidateTriplets);
		passIIResult.setTotalTransactionLines(passIResults.getTotalTransactionLines());
		passIIResult.setSupport(this.support);
		return passIIResult;
	}
	
	public PassNResult executePassIII(PassIIResult passIIResult) throws IOException {
		DataFileReader passReader = new DataFileReader(transactionFile.getAbsolutePath());
		IntItemCountStorage storageCount = new IntItemCountStorage();
		int nextPassNumber = 4;
		long startTime = System.currentTimeMillis();
		System.out.println("Doing pass III");
		passReader.readTransactions(passIIResult, storageCount);
		storageCount.removeInfrequentPairsAndWriteToDisk(this.support, fileWriter);
//		System.out.println(storageCount);
		
		List<IntItems> candidateFrequentItems = storageCount.generateCandidateFrequentItemsForNextPass(nextPassNumber);
//		Set<IntItems> candidateFrequentItems = storageCount.generateUniqueCandidateFrequentItemsForNextPass(nextPassNumber);
//		System.out.println(candidateFrequentItems);
		storageCount = null;
		long executionTime = System.currentTimeMillis() - startTime;
		System.out.println("ExecutionTime for passIII = " + executionTime + " ms");
		PassNResult passIIIResult =  new PassNResult(candidateFrequentItems,0,nextPassNumber);
		passIIIResult.setTotalTransactionLines(passIIResult.getTotalTransactionLines());
		passIIIResult.setSupport(this.support);
		return passIIIResult;
	}
	
	public PassNResult executePassIIICompressed(PassIIResult passIIResult) throws IOException {
		DataFileReader passReader = new DataFileReader(transactionFile.getAbsolutePath());
		IntItemCountStorage storageCount = new IntItemCountStorage();
		int nextPassNumber = 4;
		long startTime = System.currentTimeMillis();
		System.out.println("Doing pass III");
		passReader.readTransactionsPassIII(passIIResult, storageCount);
		storageCount.removeInfrequentPairsAndWriteToDisk(this.support, fileWriter);
//		System.out.println(storageCount);
		
		List<IntItems> candidateFrequentItems = storageCount.generateCandidateFrequentItemsForNextPass(nextPassNumber);
//		Set<IntItems> candidateFrequentItems = storageCount.generateUniqueCandidateFrequentItemsForNextPass(nextPassNumber);
//		System.out.println(candidateFrequentItems);
		storageCount = null;
		long executionTime = System.currentTimeMillis() - startTime;
		System.out.println("ExecutionTime for passIII = " + executionTime + " ms");
		PassNResult passIIIResult =  new PassNResult(candidateFrequentItems,0,nextPassNumber);
		passIIIResult.setTotalTransactionLines(passIIResult.getTotalTransactionLines());
		passIIIResult.setSupport(this.support);
		return passIIIResult;
	}
	
	public PassNResult executePassIIICharCompressed(PassIIResult passIIResult) throws IOException {
		DataFileReader passReader = new DataFileReader(transactionFile.getAbsolutePath());
		IntItemCountStorage storageCount = new IntItemCountStorage();
		int nextPassNumber = 4;
		long startTime = System.currentTimeMillis();
		System.out.println("Doing pass III");
		passReader.readTransactionsPassIIIChar(passIIResult, storageCount);
		storageCount.removeInfrequentPairsAndWriteToDisk(this.support, fileWriter);
//		System.out.println(storageCount);
		
		List<IntItems> candidateFrequentItems = storageCount.generateCandidateFrequentItemsForNextPass(nextPassNumber);
		storageCount = null;
		long executionTime = System.currentTimeMillis() - startTime;
		System.out.println("ExecutionTime for passIII = " + executionTime + " ms");
		PassNResult passIIIResult =  new PassNResult(candidateFrequentItems,0,nextPassNumber);
		passIIIResult.setTotalTransactionLines(passIIResult.getTotalTransactionLines());
		passIIIResult.setSupport(this.support);
		return passIIIResult;
	}
	
	public PassNResult executePassIIICharFileStorageCompressed(PassIIResult passIIResult) throws Exception {
		DataFileReader passReader = new DataFileReader(transactionFile.getAbsolutePath());
		IntItemCountStorage storageCount = new IntItemCountStorage();
		int nextPassNumber = 4;
		long startTime = System.currentTimeMillis();
//		System.out.println("Doing pass III");
		passReader.readTransactionsPassIIICharFileStorage(passIIResult, storageCount);
		storageCount.removeInfrequentPairsAndWriteToDisk(this.support, fileWriter);
//		System.out.println(storageCount);
		
		List<IntItems> candidateFrequentItems = storageCount.generateCandidateFrequentItemsForNextPass(nextPassNumber);
		storageCount = null;
		long executionTime = System.currentTimeMillis() - startTime;
//		System.out.println("ExecutionTime for passIII = " + executionTime + " ms");
		PassNResult passIIIResult =  new PassNResult(candidateFrequentItems,0,nextPassNumber);
		passIIIResult.setTotalTransactionLines(passIIResult.getTotalTransactionLines());
		passIIIResult.setSupport(this.support);
		return passIIIResult;
	}
	
	
	public PassNResult executePassN(PassNResult previousPassNResult) throws IOException {
		DataFileReader passReader = new DataFileReader(transactionFile.getAbsolutePath());
		IntItemCountStorage storageCount = new IntItemCountStorage();
		int nextPassNumber = previousPassNResult.getPassNumber() + 1;
		long startTime = System.currentTimeMillis();
		
//		System.out.println("Doing pass " + previousPassNResult.getPassNumber());
		//System.out.println("candidte" + previousPassNResult.getCandidateFrequentItems());
		passReader.readTransactions(previousPassNResult, storageCount);
		storageCount.removeInfrequentPairsAndWriteToDisk(this.support, fileWriter);
		List<IntItems> candidateFrequentItems = storageCount.generateCandidateFrequentItemsForNextPass(nextPassNumber);
//		Set<IntItems> candidateFrequentItems = storageCount.generateUniqueCandidateFrequentItemsForNextPass(nextPassNumber);
		storageCount = null;
		long executionTime = System.currentTimeMillis() - startTime;
//		System.out.println("ExecutionTime for pass = " + previousPassNResult.getPassNumber() + " is " + executionTime + " ms");
		PassNResult passNResult = new PassNResult(candidateFrequentItems,0,nextPassNumber);
		passNResult.setTotalTransactionLines(previousPassNResult.getTotalTransactionLines());
		passNResult.setSupport(this.support);
		return passNResult;
	}
	
	public BitVector generateBitVector(int[] itemCount,int support) {
		boolean[] bitVector = new boolean[itemCount.length];
		int biggestFrequentItem = -1;
		int previousBiggestFrequentItemCount = 0;
		int nbrFrequentItems = 0;
		for(int item = 0; item < itemCount.length ; item++){
			if (itemCount[item] >= support) {
				bitVector[item] = true;
				nbrFrequentItems++;
				if (previousBiggestFrequentItemCount <= itemCount[item] ) {
					previousBiggestFrequentItemCount = itemCount[item];
				}
				biggestFrequentItem = item;
			} 
		}
		return new BitVector(nbrFrequentItems,biggestFrequentItem, bitVector);
	}
	
	public HashBitVector[] generateHashBitVectors(HashBucket[] hashbuckets,int support) {
		HashBitVector[] hashBitVectors = new HashBitVector[hashbuckets.length];
		int index = 0;
		for(HashBucket hashBucket: hashbuckets) {
			hashBitVectors[index] = hashBucket.generateBitVector(support);
			index++;
		}
		return hashBitVectors;
	}
	
	public int calculateSupport(double supportPercentange, int numberofTransactions) {
		return (int) ((numberofTransactions * supportPercentange) / 100);
	}

}
